package test2method;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;

import javax.annotation.PostConstruct;

/**
 * @author: mornd
 * @dateTime: 2023/5/17 - 20:23
 * set 方法或属性产生的循环依赖，spring 3级缓存已经解决了
 */

@Slf4j
public class Demo {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("user", User.class);
        context.registerBean("order", Order.class);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        context.refresh();
    }

    static class User {
        private Order order;

        public User() {
            log.debug("User()");
        }

        @Autowired
        public void setOrder(Order order) {
            log.debug("user setOrder({})",order.getClass());
            this.order = order;
        }

        @PostConstruct
        public void init() {
            log.debug("user init()");
        }
    }

    static class Order {
        private User user;

        public Order() {
            log.debug("Order()");
        }

        @Autowired
        public void setUser(User user) {
            log.debug("Order setUser({})", user.getClass());
            this.user = user;
        }

        @PostConstruct
        public void init() {
            log.debug("order init()");
        }
    }
}
